package org.example.config;


import io.minio.MinioClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;

@Configuration
@EnableConfigurationProperties(S3Properties.class)
public class S3Config {

    @Bean
    @Conditional(MinioCondition.class)
    public MinioClient minioClient(S3Properties s3Properties) {
        //初始化 MinioClient，
        return MinioClient.builder()
                .endpoint(s3Properties.getEndpoint())
                .credentials(s3Properties.getAwsAccessKeyId(), s3Properties.getAwsSecretAccessKey())
                .build();
    }

    public static class MinioCondition extends SpringBootCondition {
        @Override
        public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
            Environment environment = context.getEnvironment();
            boolean match = StringUtils.isNotBlank(environment.getProperty(S3Properties.ENDPOINT));
            return new ConditionOutcome(match, "Condition1 and Condition2 are " + (match ? "matched" : "not matched"));
        }
    }
}